import React from 'react';
import dva,{connect} from './dva';
 //通过执行dva获取一个app的应用实例
const app = dva();
const delay = (ms)=> new Promise((resolve,reject)=>{
  setTimeout(()=>{
    resolve();
  },ms)
});
app.model({
  namespace:'counter',
  state: {number: 0}, //初始状态
  reducers:{ //同步处理函数
    add(state,action){
      return {number: state.number + 1}
    }
  },
  //effects中都是generator生成器
  effects: {
    *asyncAdd({put,call,select},action){
      yield call(delay,1000);
      yield put({type:'counter/add'});//put派发一个动作
      let state = yield select();
      console.log(state)
    }
  }

})

const Counter = connect(
  state=>state.counter
)(props=>{
  return <div>
    <p>{props.number}</p>
    <button onClick={()=>{props.dispatch({type:'counter/add'})}}>add</button>
    <button onClick={()=>{props.dispatch({type:'counter/asyncAdd'})}}>asyncAdd</button>
  </div>
});
//配置路由
app.router(()=><Counter/>);
//启动应用
app.start('#root');